<template>
<div>
  <div class="loader-base" v-if="showLoad"><div class="loader-wrap"></div></div>
  <div class="logo-bg2"><img src="/static/img/logo_icon.png"></div>
  <ul class="login-wrap">
    <li class="displayflex">
      <span class="iconfont icon-tel"></span>
      <div class="flex1 login-ipt"><input type="tel" placeholder="手机号" v-model.trim="username"></div>
      <span class="iconfont icon-del" v-if="username" @click="username=''"></span>
    </li>
    <li class="displayflex">
      <span class="iconfont icon-password"></span>
      <div class="flex1 login-ipt"><input type="password" placeholder="密码" v-model.trim="password"></div>
      <span class="iconfont icon-del" v-if="password" @click="password=''"></span>
    </li>
  </ul>
  <div class="btn-w90 mt60">
    <button class="btn btn-default" :class="{'btn-click':clickbtn}" @click="login">登 录</button>
  </div>
  <div class="login-bottom cf">
    <router-link :to="{ path: '/register'}" class="f-l">配送员注册</router-link>
    <router-link :to="{ path: '/password'}" class="f-r">忘记密码</router-link>
    <!-- <router-link :to="{ path: '/module'}" style="display:block; width:100%; text-align:center">module</router-link> -->
  </div>
  <!-- 弹框 -->
  <x-dialog v-model="showDialogStyle" :dialog-style="{'max-width': '100%', width: '90%', 'min-height': '50%', 'background-color': 'transparent'}">
    <div class="role-box">
      <h2>请选择您的登录角色</h2>
      <!-- <p>选择角色后将不得更改，请谨慎选择</p> -->
      <ul class="role-list">
        <li @click="gotoMsg('Cargowner')"><span class="icon truck"></span><em class="name">我是货主</em></li>
        <!-- <li @click=""><span class="icon storage"></span><em class="name">我是仓管员</em></li> -->
        <li @click="gotoMsg('Driver')"><span class="icon driver"></span><em class="name">我是配送员</em></li>
        <!-- <li @click=""><span class="icon peoson"></span><em class="name">我是收货方</em></li> -->
      </ul>
    </div>
  </x-dialog>
</div>
</template>
<script>
import md5 from "js-md5";
export default {
  data() {
    return {
      username: "",
      password: "",
      clickbtn: false,
      showDialogStyle: false,
      UserInfo: [],
      showLoad: false,
      auto: false
    };
  },
  watch: {
    password(val) {
      if (val && this.username) {
        this.clickbtn = true;
      } else {
        this.clickbtn = false;
      }
    },
    username(val) {
      if (val && this.password) {
        this.clickbtn = true;
      } else {
        this.clickbtn = false;
      }
    }
  },
  computed: {
    code() {
      if (localStorage.wechatCode) {
        return localStorage.wechatCode;
      }
    }
  },
  created() {
    this.getData();
    sessionStorage.clear();
  },
  methods: {
    getData() {
      if (localStorage.UserInfo) {
        var _this = this;
        var userinfo = JSON.parse(localStorage.UserInfo);
        this.username = userinfo.LogonUser;
        this.password = userinfo.Pwd;
        this.auto = true;
        this.login();
      }
    },
    login() {
      var reg = /^1[345789]\d{9}$/;
      if (this.username == "" || !this.username) {
        this.$vux.toast.show({
          type: "warn", //success, warn, cancel, text
          text: "请填写手机号"
        });
        return false;
      } else if (!reg.test(this.username)) {
        this.$vux.toast.show({
          type: "warn", //success, warn, cancel, text
          text: "请填写正确手机号"
        });
        return false;
      }

      if (!this.password) {
        this.$vux.toast.show({
          type: "warn", //success, warn, cancel, text
          text: "请填写密码"
        });
        return false;
      }

      this.showLoad = true;
      var _this = this,
        query = {
          LogonUser: this.username,
          Code: this.code
        };
      if (_this.auto) {
        query.Auto = "auto";
      } else {
        query.Pwd = md5(_this.password).toUpperCase();
      }
      this.Ajax.post("UserLogin", query)
        .then(function(resp) {
          if (resp.IsSuccess) {
            localStorage.removeItem("wechatCode");
            localStorage.UserInfo = JSON.stringify(resp.Data);
            _this.UserInfo = resp.Data;
            // if (
            //   _this.UserInfo.UserBizType == null ||
            //   _this.UserInfo.UserBizType == ""
            // ) {
            //   _this.showDialogStyle = true;
            // }
            //else {
            if (_this.UserInfo.UserBizType == "Driver") {
              _this.$router.push({
                path: "/driver"
              });
            } else if (
              _this.UserInfo.UserBizType == "Cargoreceiver" ||
              _this.UserInfo.UserBizType == "warehouse" ||
              _this.UserInfo.UserBizType == "Cargowner"
            ) {
              //  else if (_this.UserInfo.UserBizType == "Operation") {
              //   _this.$router.push({
              //     path: "/operation"
              //   });
              // }
              if (
                _this.UserInfo.UserBizType == "Cargowner" &&
                _this.UserInfo.TenantFlag == "N"
              ) {
                _this.$router.push({
                  path: "/msg"
                });
              } else {
                //增加判断是否有订单GUID参数，如果有，则表示需要跳转到订单页
                if(_this.$route.query.bbillGuid && _this.$route.query.bbillGuid != '')
                {
                  _this.$router.replace({
                    path: "/sign",
                    query: { id: _this.$route.query.bbillGuid },
                  });
                }
                else
                {
                  _this.$router.push({
                    path: "/index"
                  });
                }
              }
            } else {
              _this.showLoad = false;
              _this.$vux.toast.show({
                type: "warn", //success, warn, cancel, text
                text: "账号不存在"
              });
            }
            // }
          } else {
            _this.showLoad = false;
            _this.$vux.toast.show({
              type: "warn", //success, warn, cancel, text
              text: resp.ErrorMessage
            });
          }
        })
        .catch(error => {
          _this.showLoad = false;
          _this.$vux.toast.show({
            type: "warn", //success, warn, cancel, text
            text: "网络连接失败"
          });
        });
    },
    gotoMsg(userbiztype) {
      var _this = this,
        query = {
          StaffID: _this.UserInfo.StaffID,
          UserBizType: userbiztype
        };
      this.Ajax.post("SelectUserBizType", query).then(function(resp) {
        if (resp.IsSuccess) {
          localStorage.UserInfo = JSON.stringify(resp.Data);
          _this.UserInfo = resp.Data;
          if (_this.UserInfo.UserBizType == "Driver") {
            _this.$router.push({
              path: "/driver"
            });
          } else if (_this.UserInfo.UserBizType == "Cargowner") {
            _this.$router.push({
              path: "/msg"
            });
          } else {
            _this.$vux.toast.show({
              type: "warn", //success, warn, cancel, text
              text: "未知错误"
            });
          }
        } else {
          _this.$vux.toast.show({
            type: "warn", //success, warn, cancel, text
            text: resp.ErrorMessage
          });
        }
      });
    },
    ConditionRedirect(id, type)
    {
      
    }
  }
};
</script>